package com.kele.controller;

import com.alibaba.fastjson.JSON;
import com.kele.entity.Document;
import com.kele.service.impl.DocumentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2020/11/16
 * @Content:
 * @Description：
 */
@Controller
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    private DocumentServiceImpl documentService;

    /**
     * 新增文档
     */
    @PostMapping("/addDocument")
    @ResponseBody
    public Map<String,Object> addDocument(@RequestParam("cust_name") String cust_name,
                                          @RequestParam("title") String title,
                                          @RequestParam("desc") String desc,
                                          @RequestParam("create_time") Date create_time){

        Map<String,Object> map=new HashMap<>();
        Document document=new Document();
        document.setCust_name(cust_name);
        document.setTitle(title);
        document.setDesc(desc);
        document.setCreate_time(create_time);
        boolean isSuccess = documentService.addDocument(document);
        map.put("isSuccess",isSuccess);
        return map;


    }

    /**
     * 查找所有
     *
     */
    @RequestMapping("/getAllDocument")
    @ResponseBody
    public String getAllDocument() {
        List<Document> list = documentService.getAll();
        String JsonString = JSON.toJSONString(list);
        System.out.println(JsonString);
        return JsonString;
    }


    /**
     *通过id查找
     */
    @RequestMapping("/findDocumentById")
    @ResponseBody
    public String findDocumentById(int document_id) {
        Document document=null;
        document = documentService.findById(document_id);
        String json = JSON.toJSONString(document);
        System.out.println(json);
        return json;
    }

    /**
     * 修改
     */
    @RequestMapping("/editDocument")
    @ResponseBody
    public Map<String, Object> editDocument(int document_id, String cust_name, String title, String desc,  Date create_time) {
        Map<String, Object> map = new HashMap<>();
        Document document = documentService.findById(document_id);
        System.out.println(JSON.toJSONString(document));
        document.setDocument_id(document_id);
        document.setCust_name(cust_name);
        document.setTitle(title);
        document.setDesc(desc);
        document.setCreate_time(create_time);
        boolean isSuccess = documentService.editDocument(document);
        map.put("isSuccess", isSuccess);
        return map;
    }

    /**
     * 删除
     */
    @RequestMapping("/delDocument")
    public String delDocument(int document_id) {
         documentService.delDocument(document_id);
        return "document-list";
    }

    /**
     * 通过用户名去查找文档信息
     * @param cust_name
     * @return
     */
    @RequestMapping("/getDocumentByCustName")
    @ResponseBody
    public String getDocumentByCustName(String cust_name) {
        List<Document> list = documentService.getDocumentByCustName(cust_name);
        String json = JSON.toJSONString(list);
        System.out.println(json);
        return json;
    }
}
